package tnviet.proj.jna.analyzer;
import jpcap.packet.*;
import java.util.*;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;

public class TCPAnalyzer extends JNPacketAnalyzer
{
    private static ResourceManager resourceManager = new ResourceManager(JNPacketAnalyzer.class, Program.locale);
	private static final String[] valueNames={
		resourceManager.getString("tcp.sourcePort"),
		resourceManager.getString("tcp.destPort"),
		resourceManager.getString("tcp.sequenceNumber"),
		resourceManager.getString("tcp.ackNumber"),
		resourceManager.getString("tcp.urgFlag"),
		resourceManager.getString("tcp.ackFlag"),
		resourceManager.getString("tcp.pshFlag"),
		resourceManager.getString("tcp.rstFlag"),
		resourceManager.getString("tcp.synFlag"),
		resourceManager.getString("tcp.finFlag"),
		resourceManager.getString("tcp.windowSize")
        };
	Hashtable values=new Hashtable();
	
	public TCPAnalyzer(){
		layer=TRANSPORT_LAYER;
	}
	
	public boolean isAnalyzable(Packet p){
		return (p instanceof TCPPacket);
	}
	
	public String getProtocolName(){
		return resourceManager.getString("tcp");
	}
	
	public String[] getValueNames(){
		return valueNames;
	}
	
	public void analyze(Packet p){
		values.clear();
		if(!isAnalyzable(p)) return;
		TCPPacket tcp=(TCPPacket)p;
		values.put(valueNames[0],new Integer(tcp.src_port));
		values.put(valueNames[1],new Integer(tcp.dst_port));
		values.put(valueNames[2],new Long(tcp.sequence));
		values.put(valueNames[3],new Long(tcp.ack_num));
		values.put(valueNames[4],new Boolean(tcp.urg));
		values.put(valueNames[5],new Boolean(tcp.ack));
		values.put(valueNames[6],new Boolean(tcp.psh));
		values.put(valueNames[7],new Boolean(tcp.rst));
		values.put(valueNames[8],new Boolean(tcp.syn));
		values.put(valueNames[9],new Boolean(tcp.fin));
		values.put(valueNames[10],new Integer(tcp.window));
	}
	
	public Object getValue(String valueName){
		return values.get(valueName);
	}
	
	Object getValueAt(int index){
		if(index<0 || index>=valueNames.length) return null;
		return values.get(valueNames[index]);
	}
	
	public Object[] getValues(){
		Object[] v=new Object[valueNames.length];
		
		for(int i=0;i<valueNames.length;i++)
			v[i]=values.get(valueNames[i]);
		
		return v;
	}
        @Override
        public String getValueNameAt(int index) {
            return valueNames[index];
        }
}
